--
-- Author: ping.wang
-- Date: 2019-05-29 11:53:26
--
-- QuickLevelView
--

local AvatarFactory = app:getClass("AvatarFactory")
local Armature = app:getClass("Armature")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local NodeSetVisibleFunc = cc.Node.setVisible
local NodeIsVisibleFunc = cc.Node.isVisible
ClassRef.VIEW_ROOT_RES = Res.MountNewScene

function ClassRef:init()
    self._ui["Panel/Panel_bg/Image_1"]:setContentSize(display.size)

    self._ui.Button_1:addClickEventListener(function()
        self._ui.Button_1:hide()
        self._ui.Button_2:hide()
        local pc = app:getInst("PhotoController")
        pc:snapshotNode(self)
        local act = cca.seq({cca.delay(0), cca.callFunc(function()
            -- UIHandler:getPopupUi():removeUIByKey(ClassRef:getViewName())
            pc:showPhotoViewShare()
            self._ui.Button_1:show()
            self._ui.Button_2:show()
        end), })

        self._ui.Button_1:runAction(act)
    end)
    self._ui.Button_2:addClickEventListener(function()
        executeFunc(self._onCloseView, self)
    end)

    self._starTimeline = self._ui["Panel/light"]:getTimeline()
    self._starTimeline:gotoFrameAndPause(0)
end

function ClassRef:show()
    self:setVisible(true)
    self._root:setVisible(false)

    local params = {
        armatureName = "getride",
        path = Res.MountNewEfcPath
    }
    local armature = Armature.create(params)
    armature:setPosition(display.cx, display.cy)
    self:addChild(armature, 2)

    armature:play("getride", 1, function(sender)
        sender:removeFromParent()
    end)
    armature:onFrameEvent(function(sender, name)
        if name == "all" then
            self._root:setVisible(true)
            local params = UD:getAvatarParams()
            params.mountID = 0
            local hero = AvatarFactory.createHero(params)
            if not hero then return end
            self._ui.Node_Partner:addChild(hero)
            local size = 1
            hero:setScale(size)
            hero:play("mapidle")
            hero:setPosition(cc.p(0,0))
            self:playLevelUpAnimation(hero)
        end
    end)

    AM.playSoundById(5153)
end

function ClassRef:refresh(title,tipStr)
    self._ui.title:setString(title)
    if tipStr then
        self._ui.text:setVisible(true)
        self._ui.text:setString(tipStr)
    else
        self._ui.text:setVisible(false)
    end

end

function ClassRef:playLevelUpAnimation(parentNode)
    if not self._levelupAnimation then
        local animation = NodeFactory:createLevelUpAnimation()
        self._levelupAnimation = animation
        parentNode:addChild(animation,1)
        animation:setScale(1.5)
        NodeSetVisibleFunc(animation,false)
    end

    if not NodeIsVisibleFunc(self._levelupAnimation) then
        local function callback()
            NodeSetVisibleFunc(self._levelupAnimation,false)
        end
        NodeSetVisibleFunc(self._levelupAnimation,true)
        self._levelupAnimation:play(Res.Effect_LevelUp.action,1,callback)
    end
end

return ClassRef
